home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1091 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  5.9 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: kanze@gabi-soft.fr (J. Kanze)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: sample auto_ptr template
  5. Date: 15 Apr 1996 09:37:10 PDT
  6. Organization: GABI Software, Sarl.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <KANZE.96Apr15103504@gabi.gabi-soft.fr>
  9. References: <009A04DA6A831C40.49800EAC@ittpub.nl>
  10.     <bill-0804960932250001@bgibbons.vip.best.com> <4kcr2d$p03@jabba.lehman.com>
  11.     <KANZE.96Apr10111407@gabi.gabi-soft.fr>
  12.     <199604121609.MAA27937@jabba.lehman.com>
  13. NNTP-Posting-Host: isolde.mti.sgi.com
  14. X-Original-Date: 15 Apr 1996 08:35:04 GMT
  15. In-reply-to: ajay@lehman.com's message of 12 Apr 96 17:36:33 GMT
  16. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  17.     iQBVAwUBMXJ7OEy4NqrwXLNJAQENDwH/WL2aytFfdoH2SApQt4xAqyfMDcjX2Ueh
  18.     J0A9rCVfX4i8DWVVi02bS/rRpIcRJI/JbI0CPzrmfigdTKz39DRoGA==
  19.     =81UW
  20. Originator: austern@isolde.mti.sgi.com
  21.  
  22. In article <199604121609.MAA27937@jabba.lehman.com> ajay@lehman.com
  23. (Ajay Kamdar) writes:
  24.  
  25. |> In article <KANZE.96Apr10111407@gabi.gabi-soft.fr>,
  26. |> J. Kanze <kanze@gabi-soft.fr> wrote:
  27. |> >In article <4kcr2d$p03@jabba.lehman.com> ajay@lehman.com (Ajay Kamdar)
  28. |> >writes:
  29. |> >
  30. |> >|> You didn't say whether the example I gave was exception
  31. |> >|> safe or not.
  32. |> >
  33. |> >But I did.  It's not.
  34. |> >
  35. |> >|> To reiterate the main points of the example
  36. |> >|>    + The caller directly initializes an auto_ptr with
  37. |> >|>      the returned value from the calee.
  38. |> >|>      auto_ptr<X> ptr(get_X());
  39. |> >
  40. |> >|>    + The callee releases the pointer from it's own
  41. |> >|>      auto_ptr in it's return statement.
  42. |> >
  43. |> >|> No copy constructors are involved in the return. Hence
  44. |> >|> there is no possibility of an exception thrown from
  45. |> >|> a user defined copy constructor. So which window are
  46. |> >|> you referring to during which there is no exception
  47. |> >|> safety? I claim there is none.
  48. |> >
  49. |> >The destructors of any local variables in get_X.
  50.  
  51. |> Yes, you are right that the destructors of local variables in
  52. |> get_X() could propagate exceptions. I missed that in my initial
  53. |> analysis. However, in a previous article I believe I have shown
  54. |> clearly that propagating exceptions out of destructors is an
  55. |> extremely poor practice. Depending upon whether the destruction
  56. |> of remaining stack variables in get_X() is attempted if one of
  57. |> the statck variables throws an exception, the program will either
  58. |> terminate or will likely leak memory. I think it would be a
  59. |> dubious choice to make auto_ptr less safe to use by making it have
  60. |> copy semantics to support such off beat no-win cases.
  61.  
  62. Well, I agree fully that it is bad programming practice to let
  63. exceptions propagate out of a destructor.  At present, however, it is
  64. legal C++ (although if the committee were to declare it undefined
  65. behavior, they wouldn't get any objections from me), and as such, should
  66. be taken into consideration when designing a *standard* library
  67. component.  (I *don't* take it into consideration when designing my own
  68. components.  But: if you don't like my style, you aren't obliged to use
  69. my components.  Everyone has to use the standard components, however.)
  70.  
  71. |>                 [snip]
  72. |> >
  73. |> >|> So which one is better for the C++ programmer? An approach
  74. |> >|> which can lead to disastorous surprises at run-time
  75. |> >|> (auto_ptr with copy semantics approach) definitely does
  76. |> >|> not look the right choice under any stretch of imagination.
  77. |> >
  78. |> >In short, you are suggesting that memory leaks are OK, as long as they
  79. |> >don't occur too often.  This isn't the case for my applications.
  80.  
  81.  
  82. |> Applications which cannot tolerate even occasional memory leaks
  83. |> when exceptions propagate through destructors (if they are
  84. |> lucky to live past those exceptions) should go through sufficient
  85. |> testing to find all such occurences. Enough memory leak detection
  86. |> tools exist on the market to make this possible. The onus
  87. |> of finding those leaks is on those applications. Alternatively
  88. |> they could use garbage collection. Given that even an auto_ptr
  89.                   ^^^^^^^^^^^^^^^^^^
  90.  
  91. Would that it were.  But if this were really true (for portable
  92. programs), then we wouldn't have to argue the point about auto_ptr,
  93. would we?  There'd be no need of the class in any of its forms.  (I'll
  94. trade you namespaces, RTTI and exceptions for garbage collection.)
  95.  
  96. |> having copy semantics does not really solve the problem with
  97. |> exceptions emnating from destructors, I have a hard time buying
  98. |> into warping the auto_ptr design and making it unsafe for
  99. |> everyone to support such applications.
  100.  
  101. |> Moreover, if the unsafe copy semantics are moved out of auto_ptr
  102. |> into taligent_ptr, applications which are willing to accept
  103. |> the unsafe semantics and the lack of a complete solution inspite
  104. |> of sacrificing safety can always use the taligent_ptr. But it
  105. |> would be a *choice* that is not imposed upon everyone using the
  106. |> standard auto_ptr class.
  107.  
  108. I'm not going to reiterate my arguments, since I really have nothing to
  109. add.  I understand your position, and in fact largely agree with the
  110. points you have made.  I just feel that there are other issues involved
  111. which are more important.  (I also feel that the only real solution is
  112. garbage collection, which allows the semantics I want with the safety
  113. you want.  Maybe if commercial providers will start implementing
  114. something like the Boehm collector as an extension, we will have the
  115. existing practice necessary to get it into the next revision.)
  116. -- 
  117. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  118. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  119. Conseils en informatique industrielle --
  120.                             -- Beratung in industrieller Datenverarbeitung
  121. ---
  122. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  123.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  124.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  125.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  126.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  127. ]
  128.